/* eslint-disable import/no-extraneous-dependencies */
import axios from 'axios';
import { ElMessage } from 'element-plus';

// 创建axios实例
const request = axios.create({
  baseURL: 'http://127.0.0.1:3000',
});

// 添加请求拦截器
request.interceptors.request.use(
  (config) => {
    const conf = config;
    // 在发送请求之前做些什么
    conf.headers.authorization = localStorage.token;
    return conf;
  },
  (error) => Promise.reject(error),
);

// 添加响应拦截器
request.interceptors.response.use(
  (response) => {
    const res = response;
    return res;
  },
  (error) => {
    ElMessage.error(error.response.data.message);
    return Promise.reject(error);
  },
);

export default request;
